package com.ewzj.bigdata.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.di.component.DaggerCollectListComponent;
import com.ewzj.bigdata.di.module.CollectListModule;
import com.ewzj.bigdata.mvp.contract.CollectListContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.CollectBean;
import com.ewzj.bigdata.mvp.model.entity.resbean.NewBean;
import com.ewzj.bigdata.mvp.presenter.CollectListPresenter;
import com.ewzj.bigdata.mvp.ui.adapter.NewsAdapter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class CollectListActivity extends BaseActivity<CollectListPresenter> implements CollectListContract.View {

    @BindView(R.id.rv_content)
    RecyclerView rvContent;
    @BindView(R.id.iv_collectimg)
    ImageView ivcollectimg;
    @BindView(R.id.toolbar_title)
    TextView toolbartitle;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    private NewsAdapter newsAdapter;
    private List<NewBean> newBeans;
    private CollectBean collectBean;
    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerCollectListComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .collectListModule(new CollectListModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(Bundle savedInstanceState) {
        return R.layout.activity_collectlist; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        collectBean=getIntent().getParcelableExtra(AppConstants.EXTRA_DATA);
        if (collectBean==null){
            killMyself();
        }
        toolbartitle.setVisibility(View.GONE);
        ivcollectimg.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(collectBean.getThumb())){
            ArmsUtils.obtainAppComponentFromContext(this).imageLoader()
                    .loadImage(this, ImageConfigImpl.builder()
                            .url(collectBean.getThumb())
                            .imageView(ivcollectimg)
                            .transformation(new CircleCrop())
                            .build());
        }
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPresenter.refhreData();
            }
        });
        initadapter();
        mPresenter.loadinitdata();
    }

    private void initadapter() {
        if (newBeans == null) {
            newBeans = new ArrayList<>();
        }
        if (newsAdapter == null) {
            newsAdapter = new NewsAdapter(newBeans);
        }
        rvContent.setLayoutManager(new LinearLayoutManager(this));
        rvContent.setAdapter(newsAdapter);
        newsAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.loadmoredata();
            }
        },rvContent);
    }


    @Override
    public void showLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void notifyNewsAdd(List<NewBean> newBeans) {
        if (newsAdapter!=null){
            newsAdapter.addData(newBeans);
        }
    }

    @Override
    public BaseQuickAdapter obtainAdapter() {
        if (newsAdapter==null){
            newsAdapter=new NewsAdapter(newBeans);
        }
        return newsAdapter;
    }

    @Override
    public CollectBean obtainCollectBean() {
        return collectBean;
    }

    @Override
    public void refreshNewsChange(List<NewBean> newBeans) {
        if (newsAdapter==null){
            newsAdapter=new NewsAdapter(newBeans);
        }
        newsAdapter.setNewData(newBeans);
    }
}
